﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProjectorTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.Resize += new EventHandler(MainForm_Resize);
            System.IO.StreamReader sr = new System.IO.StreamReader("ColorPattern.cfg");
            this.ptn = new Queue<string[]>(
                sr.ReadToEnd().Split(new string[] { "\r\n\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                .Select(s => s.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)));
        }

        private Bitmap bg;
        private Graphics bgg;
        private Queue<string[]> ptn;

        void MainForm_Resize(object sender, EventArgs e)
        {
            lock (this)
            {
                if (this.bg != null)
                {
                    this.BackgroundImage = null;
                    this.bg.Dispose();
                }
                this.bg = new System.Drawing.Bitmap(this.Width, this.Height);
                if (this.bgg != null)
                {
                    this.bgg.Dispose();
                }
                this.bgg = Graphics.FromImage(this.bg);
                this.bgg.Clear(Color.White);
                this.BackgroundImage = this.bg;
            }
        }

        private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == ' ')
            {
                string[] sa = this.ptn.Dequeue();
                this.ptn.Enqueue(sa);
                lock (this)
                {
                    this.FillInPattern(sa[0], sa[1]);
                }
                this.Invalidate();
            }
            if (e.KeyChar == 'q')
            {
                this.Close();
            }
        }

        private void FillInPattern(string VPs, string HPs)
        {
            Color[] VP = VPs.Split(';')//split color
                .Select(colorpattern => colorpattern.Split(',').Select(cs => int.Parse(cs)).ToArray())//split rgb
                .Select(colorints => Color.FromArgb(colorints[0], colorints[1], colorints[2]))
                .ToArray();

            Color[] HP = HPs.Split(';')//split color
                .Select(colorpattern => colorpattern.Split(',').Select(cs => int.Parse(cs)).ToArray())//split rgb
                .Select(colorints => Color.FromArgb(colorints[0], colorints[1], colorints[2]))
                .ToArray();

            int vc = 0;
            for (int i = 0; i < this.bg.Height; i++)
            {
                if (VP[vc] != Properties.Settings.Default.TransparentColor)
                {
                    Pen p = new Pen(VP[vc], 1);
                    this.bgg.DrawLine(p, 0, i, this.bg.Width, i);
                }
                vc++;
                if (vc >= VP.Length)
                {
                    vc = 0;
                }
            }

            int hc = 0;
            for (int i = 0; i < this.bg.Width; i++)
            {
                if (HP[hc] != Properties.Settings.Default.TransparentColor)
                {
                    Pen p = new Pen(HP[hc], 1);
                    this.bgg.DrawLine(p, i, 0, i, this.bg.Height);
                }
                hc++;
                if (hc >= HP.Length)
                {
                    hc = 0;
                }
            }
        }

        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {

        }
    }
}
